package cn.tedu.eb_film.service.impl;


import cn.tedu.eb_film.ex.ServiceException;
import cn.tedu.eb_film.mapper.CsParkingRateMapper;
import cn.tedu.eb_film.pojo.dto.CsParkingRateAddNewDTO;
import cn.tedu.eb_film.pojo.dto.CsParkingRateUpdateDTO;
import cn.tedu.eb_film.pojo.entity.CsParkingRate;
import cn.tedu.eb_film.pojo.entity.MovieType;
import cn.tedu.eb_film.pojo.entity.User;
import cn.tedu.eb_film.pojo.vo.CsParkingRateListItemVO;
import cn.tedu.eb_film.pojo.vo.CsParkingRateStandardVO;


import cn.tedu.eb_film.pojo.vo.MovieTypeStandardVO;
import cn.tedu.eb_film.pojo.vo.UserStandardVO;
import cn.tedu.eb_film.service.CsParkingRateService;
import cn.tedu.eb_film.utils.ServiceCode;
import cn.tedu.eb_film.utils.StateCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 停车规则表(CsParkingRate)表服务实现类
 *
 * @author HおE
 * @since 2023-03-17 18:48:53
 */
@Slf4j
@Service("csParkingRateService")
public class CsParkingRateServiceImpl implements CsParkingRateService {

    @Autowired
    private CsParkingRateMapper csParkingRateMapper;

    @Override
    public void setEnable(Integer id) {
        log.debug("开始处理【启用电影类型】的业务，参数：{}", id);
        updateEnableById(id, 1);
    }

    @Override
    public void setDisable(Integer id) {
        log.debug("开始处理【禁用电影类型】的业务，参数：{}", id);
        updateEnableById(id, 0);
    }

    private void updateEnableById(Integer id, Integer enable) {
        // 调用Mapper对象的getStandardById()方法执行查询
        CsParkingRateStandardVO currentRate = csParkingRateMapper.selectById(id);
        // 判断查询结果是否为null，如果是，则抛出异常
        if (currentRate == null) {
            String message = ENABLE_TEXT[enable] + "停车规范失败，电影类型数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 判断查询结果中的enable与参数enable是否相同，如果是，则抛出异常（当前状态与目标状态相同，没必要执行更新）
        if (currentRate.getState().equals(enable)) {
            String message = ENABLE_TEXT[enable] + "停车规范失败，此电影类型已经处于" + ENABLE_TEXT[enable] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        // 创建MovieType对象
        CsParkingRate updateRateType = new CsParkingRate();
        // 向updateMovieType对象中封装属性值：id, enable，均来自方法参数
        updateRateType.setId(id);
        updateRateType.setState(enable);
        System.out.println("updateMovieType"+updateRateType);
        // 调用Mapper对象的update()方法执行更新
        int rows = csParkingRateMapper.updateById(updateRateType);
        if (rows != 1) {
            String message = ENABLE_TEXT[enable] + "停车规范失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }


    /**
     * 新增数据
     *
     * @param csParkingRateAddNewDTO 实例对象
     * @return 实例对象
     */
    @Override
    public void insert(CsParkingRateAddNewDTO csParkingRateAddNewDTO) {
        log.debug("开始处理添加停车规则，参数为：{}",csParkingRateAddNewDTO);
        int row = csParkingRateMapper.countByConnect(csParkingRateAddNewDTO.getConnect());
        log.debug("查询插入内容是否存在，返回结果为：{}",row);
        if (row>0){
            String message = "添加失败，内容不能重复！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DATA_PRESENCE,message);
        }

        CsParkingRate csParkingRate = new CsParkingRate();
        BeanUtils.copyProperties(csParkingRateAddNewDTO,csParkingRate);
        csParkingRate.setCreateTime(LocalDateTime.now());
        log.debug("即将插入停车规则，参数为：{}",csParkingRate);
        row = csParkingRateMapper.insert(csParkingRate);
        if (row!=1){
            String message = "添加失败，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN,message);
        }
        log.debug("插入停车规则成功，参数为：{},",csParkingRate);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public void deleteById(Integer id) {
        log.debug("即将执行删除操作，id为：{}",id);
        CsParkingRateStandardVO csParkingRateStandardVO = csParkingRateMapper.selectById(id);
        log.debug("通过id查询数据，参数为：{}",csParkingRateStandardVO);
        if (csParkingRateStandardVO==null){
            String message = "删除失败，数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        int row = csParkingRateMapper.deleteById(id, StateCode.STATE_DELETE.getValue());
        if (row!=1){
            String message = "删除失败，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN,message);
        }
        CsParkingRateStandardVO csParkingRateStandardVO1 = csParkingRateMapper.selectById(id);
        log.debug("删除成功，数据为：{}",csParkingRateStandardVO1);
    }

    /**
     * 通过ID修改数据
     *
     * @param csParkingRateUpdateDTO 实例对象
     * @return 实例对象
     */
    @Override
    public void updateById(Integer id,CsParkingRateUpdateDTO csParkingRateUpdateDTO) {
        log.debug("开始执行修改业务,id为：{}，参数为：{}",id,csParkingRateUpdateDTO);
        CsParkingRateStandardVO csParkingRateStandardVO = csParkingRateMapper.selectById(id);
        log.debug("根据id查询停车规则,参数为：{}",csParkingRateStandardVO);
        if (csParkingRateStandardVO==null){
            String message = "修改停车规则失败，停车规则不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        String connect = csParkingRateUpdateDTO.getConnect();
        int rows = csParkingRateMapper.countByConnect(connect);
        log.debug("根据停车规则内容【{}【统计数量，结果为：{}",connect,rows);
        if (rows>0){
            String message = "修改停车规则内容失败，停车规则内容已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DATA_PRESENCE,message);
        }

        CsParkingRate csParkingRate = new CsParkingRate();
        BeanUtils.copyProperties(csParkingRateUpdateDTO,csParkingRate);
        csParkingRate.setId(id);
        log.debug("即将修改停车规则数据，参数为：{}",csParkingRate);
        rows = csParkingRateMapper.updateById(csParkingRate);
        if (rows!=1){
            String message = "修改停车规则失败，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN,message);
        }
        log.debug("修改数据成功，修改后参数为：{}",csParkingRate);


    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CsParkingRateStandardVO selectById(Integer id) {
        CsParkingRateStandardVO csParkingRateStandardVO = csParkingRateMapper.selectById(id);
        if (csParkingRateStandardVO==null || csParkingRateStandardVO.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = "查询停车规则失败，数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return csParkingRateStandardVO;
    }

    /**
     * 查询数据列表
     *
     * @return 实例对象列表
     */
    @Override
    public List<CsParkingRateListItemVO> selectList() {
        List<CsParkingRateListItemVO> list = csParkingRateMapper.selectList();
        list.removeIf(CsParkingRate -> CsParkingRate.getState()==(StateCode.STATE_DELETE.getValue()));
        return list;
    }

}
